package com.hz.us.controller;

import com.hz.api.model.request.ApiResponse;
import com.hz.us.model.enums.UserTypeEnum;
import com.hz.us.service.UserManagerService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("/user-manager")
@RestController
public class UserManagerController {

    @Resource
    UserManagerService userManagerService;


    @PostMapping("/add-grade")
    public String addGrade(@RequestParam("gradeName") String gradeName, @RequestParam("gradeYear") String gradeYear) {
        int add = userManagerService.addGrade(gradeName, gradeYear);
        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }

    @PostMapping("/add-class")
    public String addClass(@RequestParam("className") String className, @RequestParam("gradeId") Integer gradeId) {
        int add = userManagerService.addClass(className, gradeId);
        return add > 0 ? ApiResponse.ok(add) : ApiResponse.intError();
    }

    @PostMapping("/add-user")
    public String addUser(@RequestParam("username") String username, @RequestParam(value = "realname") String realname,
                          @RequestParam("password") String password, @RequestParam("userType") Integer userType,
                          @RequestParam(value = "schoolId", required = false) String schoolId,
                          @RequestParam(value = "gradeId", required = false) Integer gradeId,
                          @RequestParam(value = "classId", required = false) Integer classId) {
        int result = 0;
        if (userType.equals(UserTypeEnum.MANAGER.getUserType())) {
            result = userManagerService.addManager(username, realname, password);
        } else if (userType.equals(UserTypeEnum.TEACHER.getUserType())) {
            result = userManagerService.addTeacher(username, realname, password, schoolId);
        } else if (userType.equals(UserTypeEnum.STUDENT.getUserType())) {
            result = userManagerService.addStudent(username, realname, password, schoolId, gradeId, classId);
        }
        return result > 0 ? ApiResponse.ok(result) : ApiResponse.intError();
    }


}
